﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.DBM;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.SECURITY.BLL;
using System.Data;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class Login : System.Web.UI.Page
    {
        private ISecurityUserBLL userBLL = new SecurityUserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                switch (GetSystemName())
                {
                    case "DEP":
                    default:
                        headerImage.ImageUrl = "~/App_UI/Images/PageView/Header.jpg";
                        break;
                    case "ITPF":
                        headerImage.ImageUrl = "~/App_UI/Images/PageView/Header_ITPF.jpg";
                        break;
                }
                txtUserID.Focus();
                if (!String.IsNullOrEmpty(Request["username"]) && !String.IsNullOrEmpty(Request["password"]))
                {
                    LoginProcessStart(Request["username"], Request["password"]);
                }
            }
        }

		private void SingleSignOn()
		{
			// 1. Check is single signon
			string username = Request["username"];
			string password = Request["password"];

			if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
				// go to check login and redirect if login valid.
				LoginProcessStart(username, password);

			// Else = Show login UI without redirect to Default.apx
		}

        protected string GetSystemName()
        {
            IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

            try
            {
                string strResult = "";
                string sql = "select system_name"
                    + " from sys_system_controls";
                DataTable dt = new DataTable();
                dbManager.Open();
                dt = dbManager.ExecuteDataSet(CommandType.Text, sql).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    strResult = dt.Rows[0][0].ToString();
                }

                return strResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dbManager.Dispose();
            }
        }


		protected void cmdLogin_Click(object sender, EventArgs e)
		{
			LoginProcessStart();
		}

		protected void LinkButton1_Click(object sender, EventArgs e)
		{
			LoginProcessStart("administrator", "p@ssw0rd");
		}

        protected void LoginProcessStart(string userFromGet = null, string passwordFromGet = null)
        {
			string name = String.IsNullOrEmpty(userFromGet) ? txtUserID.Text.Trim() : userFromGet;
			string password = String.IsNullOrEmpty(passwordFromGet) ? txtPassword.Text.Trim() : passwordFromGet; 

            DataRowCollection drc;
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
            {
                RadManager1.Alert("กรุณากรอกรหัสผู้ใช้งานและรหัสผ่าน");
                return;
            }
            if (!userBLL.CheckPassword(name, password))
            {
                RadManager1.Alert("รอการตรวจสอบจากผู้ดูแลระบบ");
                return;
            }
            else
            {
                DataSet ds = userBLL.GetUserInfo((int?)null, name);
                if (ds != null)
                {
                    drc = ds.Tables[0].Rows;
                    if (drc.Count > 0)
                    {
                        Session["UserSeq"] = drc[0]["user_seq"].ToString();
                        Session["EmpSeq"] = drc[0]["emp_seq"].ToString();
                        Session["emp_seq"] = drc[0]["emp_seq"].ToString();
                        Session["WorkingJobSiteSeq"] = drc[0]["working_job_site_seq"].ToString();
                        Session["Height"] = hHeight.Value;
                        new SsMng().SetLoginSession(
                            (string) Session["UserSeq"],
                            (string) Session["EmpSeq"],
                            (string) Session["user_code"],
                            (string) Session["WorkingJobSiteSeq"]);
                    }
                } 
                    Page.Response.Redirect("Default.aspx");
                 
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login_Register_Form.aspx");
        }

        protected void btnForget_Click(object sender, EventArgs e)
        {
            Response.Redirect("Forget_Password.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }


    }
}
